package JVM.oom;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

// java.lang.OutOfMemoryError: Metaspace

/**
 * JVM参数：
 * -XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=10m
 */

public class MetaSpaceOOMTest {

    static class oomTest{

    }


    public static void main(String[] args) {
        int i = 0;

        try {
            while (true){
                i++;
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(oomTest.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return methodProxy.invokeSuper(o, args);
                    }
                });
                enhancer.create();
            }
        } catch (Throwable e){
            System.out.println("*******多少次后发生了异常：" + i);
            e.printStackTrace();
        }
    }
}
